﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

using FSViet.Survey.Helpers;

namespace FSViet.Survey.Administration.Site
{
	public partial class Details : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (Request.QueryString["Id"] != null)
				{
					Guid id = GlobalApplication.GetGuid(Request.QueryString["Id"]);
					if (id!=Guid.Empty)
						LoadData(id);
				}
			}
		}

		DataHelper _dataH = new DataHelper();
		void LoadData(Guid id)
		{
			TitleLabel.Text = "Update item: " + GlobalApplication.GetGuidS(id);
			try
			{
				var data = _dataH.Load("SELECT * FROM Sites WHERE Id='" + GlobalApplication.GetGuidS(id) + "'", 0, 1); 
				FillData(data.Rows[0]);

				ShowUserManage(id);
			}
			catch (Exception ex)
			{
				msg.Text = ex.Message;
#if DEBUG
				msg.Text = ex.ToString();
#endif
			}
		}

		void ShowUserManage(Guid id)
		{
			UserManageList.DataSource = _dataH.Load("SELECT aspnet_Users.UserName, UserSiteMappings.* FROM aspnet_Users INNER JOIN UserSiteMappings ON aspnet_Users.UserId = UserSiteMappings.UserId WHERE SiteId=@SiteId", 0, 100,
						_dataH.CreateParam("SiteId", SqlDbType.Char, GlobalApplication.GetGuidS(id)));

			UserManageList.DataBind();
		}

		void FillData(DataRow data)
		{
			NameInput.Text = data["Name"].ToString().Trim();
			UrlInput.Text = data["Url"]==DBNull.Value?"":data["Url"].ToString().Trim();
			RefUrlInput.Text = data["RefUrl"] == DBNull.Value ? "" : data["RefUrl"].ToString().Trim();
		}

		protected void SaveButton_Click(object sender, EventArgs e)
		{
			if (!IsValidInput()) return;

			try
			{
				Guid id = GlobalApplication.GetGuid(Request.QueryString["Id"]);

				DataHelper dataH = new DataHelper();
				if (id == Guid.Empty)
				{
					dataH.Execute("INSERT INTO Sites(Id, Name, Url, RefUrl) VALUES(@Id, @Name, @Url, @RefUrl)", 
						dataH.CreateParam("Id",SqlDbType.Char,GlobalApplication.GuidS),
						dataH.CreateParam("Name", SqlDbType.NVarChar, NameInput.Text.Trim()),
						dataH.CreateParam("Url", SqlDbType.NVarChar, UrlInput.Text.Trim()),
						dataH.CreateParam("RefUrl", SqlDbType.NVarChar, RefUrlInput.Text.Trim()));				
				}
				else
				{
					dataH.Execute("UPDATE Sites SET Name=@Name, Url=@Url, RefUrl=@RefUrl WHERE Id=@Id",
						dataH.CreateParam("Id", SqlDbType.Char, GlobalApplication.GetGuidS(id)),
						dataH.CreateParam("Name", SqlDbType.NVarChar, NameInput.Text.Trim()),
						dataH.CreateParam("Url", SqlDbType.NVarChar, UrlInput.Text.Trim()),
						dataH.CreateParam("RefUrl", SqlDbType.NVarChar, RefUrlInput.Text.Trim())); 
				}

				Response.Redirect("List.aspx");
			}
			catch (Exception ex)
			{
				msg.Text = ex.Message;
#if DEBUG
				msg.Text = ex.ToString();
#endif
			}
		}

		bool IsValidInput()
		{
			bool result = true;

			if (NameInput.Text.Trim().Length < 1)
			{
				result = false;
				msg.Text = "Please enter name";
			}

			return result;
		}

		protected void UserManageList_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				var drv = (DataRowView)e.Row.DataItem;
				var btn = e.Row.FindControl("DelUserSiteButton") as Button;
				btn.CommandName = "Del";
				btn.CommandArgument = drv["UserId"].ToString();
			}
		}

		protected void UserManageList_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName=="Del")
			{
				DataHelper dataH = new DataHelper();
				try
				{
					dataH.Execute("DELETE UserSiteMappings WHERE UserId='" + e.CommandArgument + "'");

					ShowUserManage(new Guid(Request.QueryString["Id"]));
				}
				catch (Exception ex)
				{
					msg.Text = ex.Message;
#if DEBUG
				msg.Text = ex.ToString();
#endif
				}
			}
		}
	}
}